import { setToken, getToken, removeToken } from '@/utils/auth'
import { loginInfo, getUserInfoApi } from '@/api/user'
const state = {
  token: getToken(),
  userInfo: {}
}
const mutations = {
  setUserToken(state, payload) {
    state.token = payload
  },
  getUser(state, payload) {
    state.userInfo = payload
  },
  back(state) {
    state.token = ''
    state.userInfo = {}
  }

}
const actions = {
  async login(context, payload) {
    const token = await loginInfo(payload)
    setToken(token)
    context.commit('setUserToken', token)
  },
  async getUserInfoAction(context) {
    const userInfo = await getUserInfoApi()
    context.commit('getUser', userInfo)
  },
  back(context) {
    removeToken()
    context.commit('back')
  }
}
/** 命名空间 */
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
